interface ILocToolLocation extends ng.IScope {
    /**
     * 订单详情
     */
    orderDetail:invoiceManagementOrder;
    // 定位器编号
    locToolCode:string;
    // 定位状况
    locState:string
    // 位置信息
    areaInfo:string;
    // 定位日期
    locDate:string;
   

}

// 01609090211 
function tmsLocToolLocation(locationService: ILocationService,carRecommendService:ICarRecommendService): ng.IDirective {
    return {

        templateUrl: './src/directives/tms-locToolLocation/tms-locToolLocation.html',
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
            orderDetail:"=",
            back: "&"
        },
        link: ($scope: ILocToolLocation) => {

            //   console.log($scope.orderDetail)
            
           
            setTimeout(() => {
                initMap();
                // var dotList=[{gps_lng:118.80,gps_lat:32.05},{gps_lng:118.80,gps_lat:32.30},{gps_lng:118.80,gps_lat:32.40},{gps_lng:118.80,gps_lat:32.50}];
                // drawPoint(dotList);
                // console.log(dotList);

                locationService.getLocationListByLocTool($scope.orderDetail.id).then(res=>{
                    var lastLoc=res.data.info&&res.data.info[0];
                    if(lastLoc){
                        $scope.areaInfo=lastLoc.gps_info;
                        $scope.locDate=lastLoc.gps_time;
                        $scope.locState="定位成功";
                    }
                   
                    $scope.locToolCode=res.data.gpsDeviceID
                    

                    res.data.info&&res.data.info.forEach((info,index)=>{
                        var onwayIcon= new BMap.Icon("/src/img/loctool_onway.png", new BMap.Size(40, 40));
                        var lastIcon =new BMap.Icon("/src/img/loctool_car.png", new BMap.Size(40, 57));
                        if(index===0&&$scope.orderDetail.status!=="货已送达"){
                            drawPointOnMap(info.gps_lng,info.gps_lat,lastIcon,"",index===0);
                        }else{
                            drawPointOnMap(info.gps_lng,info.gps_lat,onwayIcon,"",index===0);
                        }

                    })

                  
                },rej=>{
                    $scope.locState="定位失败"
                })

                var startLoc=$scope.orderDetail.originProvinceStr+$scope.orderDetail.originCityStr+$scope.orderDetail.originCountyStr;
                carRecommendService.getPositionFromAddress(startLoc).then(res=>{
                    // console.log(res.data);
                    var startIcon = new BMap.Icon("/src/img/loctool_start.png", new BMap.Size(40, 49));
                    drawPointOnMap(res.data.lon,res.data.lat,startIcon,'')
                })
                var endLoc=$scope.orderDetail.destinationProvinceStr+$scope.orderDetail.destinationCityStr+$scope.orderDetail.destinationCountyStr;
                carRecommendService.getPositionFromAddress(endLoc).then(res=>{
                    var endIcon= new BMap.Icon("/src/img/loctool_end.png", new BMap.Size(40, 49));
                    drawPointOnMap(res.data.lon,res.data.lat,endIcon,'')
                })
                
            }, 200);

            
            function showLocInfo(lastLoc:locToolLocationInfo){
                

                var p0 = parseFloat(lastLoc.gps_lng) ;//经度
                var p1 = parseFloat(lastLoc.gps_lat) ;//纬度
                var pt = new BMap.Point(p0, p1);
                addMarker2(pt, new BMap.Label($scope.areaInfo, { offset: new BMap.Size(39, -19)})); //offset: new BMap.Size(39, -19);
            }


            function initMap(ishide?: boolean) {
                createMap(ishide); //创建地图
                setMapEvent(); //设置地图事件
                addMapControl(); //向地图添加控件
            }

            //创建地图函数：

            let map;
            let hasMap: boolean;
            
            hasMap = false;

            function createMap(ishide?: boolean) {
               
               
                map = new BMap.Map("BDMapContent"); //在百度地图容器中创建一个地图
                let point = new BMap.Point(119.446448, 32.164418); //定义一个中心点坐标
                map.centerAndZoom(point, 7); //设定地图的中心点和坐标并将地图显示在地图容器中
                (<any>window).map = map; //将map变量存储在全局
            }

            //地图事件设置函数：
            function setMapEvent() {
                map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
            }

            //地图控件添加函数：
            function addMapControl() {
                //向地图中添加缩放控件
                let ctrl_nav = new BMap.NavigationControl({
                    anchor: BMAP_ANCHOR_TOP_LEFT,
                    type: BMAP_NAVIGATION_CONTROL_LARGE
                });
                map.addControl(ctrl_nav);
                //向地图中添加比例尺控件
                let ctrl_sca = new BMap.ScaleControl({
                    anchor: BMAP_ANCHOR_BOTTOM_LEFT
                });
                map.addControl(ctrl_sca);
            }

         

            // 编写创建标注函数
            function addMarker(point, label) {
                let marker = new BMap.Marker(point);
                map.addOverlay(marker);
                marker.setLabel(label);
            }


            //编写创建标注函数-当前定位点
            function addMarker2(point, label) {
                var myIcon = new BMap.Icon("/src/img/hc.png", new BMap.Size(59, -119));
                var marker2 = new BMap.Marker(point, { icon: myIcon });  // 创建标注
                map.addOverlay(marker2);              // 将标注添加到地图中
                marker2.setLabel(label);
                //marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                map.centerAndZoom(point, 11);
            }


            //逆地址解析函数,起始点使用
            function s_e(adds) {
                myGeo.getPoint(adds, function (point) {
                    if (point) {
                        let address = new BMap.Point(point.lng, point.lat);
                        addMarker(address, new BMap.Label(adds, {
                            offset: new BMap.Size(20, -10)
                        }));
                    }
                });
            }
            /**
             * 地图上绘制折线
             */
            function addPolyLine(pointList){
                var BMapList=pointList.map(point=>{
                    return new BMap.Point(point.lat,point.lon)
                })
                var polyLine=new BMap.Polyline(BMapList,{
                    strokeColor:"red", strokeWeight:3, strokeOpacity:1
                })

                map.addOverlay(polyLine);
            }

            function drawPoint(dotList){

                var startIcon = new BMap.Icon("/src/img/loctool_start.png", new BMap.Size(20, 20));
                var endIcon = new BMap.Icon("/src/img/loctool_end.png", new BMap.Size(20, 20));
                var onWayIcon = new BMap.Icon("/src/img/loctool_onway.png", new BMap.Size(40, 40));
                var carIcon = new BMap.Icon("/src/img/loctool_car.png", new BMap.Size(20, 20));


                dotList.forEach(dot => {
                    var p0 = parseFloat(dot.gps_lng) ;//经度
                    var p1 = parseFloat(dot.gps_lat) ;//纬度
                    var point = new BMap.Point(p0, p1);

                    var marker2 = new BMap.Marker(point, { icon: onWayIcon,title:"wocao" });  // 创建标注
                    map.addOverlay(marker2);              // 将标注添加到地图中
                });

              
             
            }

            function drawPointOnMap(lon,lat,icon,title,centerTo?:boolean){
                var p0 =lon ;//经度
                var p1 = lat ;//纬度
                var point = new BMap.Point(p0, p1);
                var options=title?{ icon: icon,title:title }:{icon: icon};

                var marker2 = new BMap.Marker(point,options);  // 创建标注
                map.addOverlay(marker2);   
                centerTo&&map.centerAndZoom(point, 11); //设定地图的中心点和坐标并将地图显示在地图容器中
            }
            

            let myGeo = new BMap.Geocoder();

            


        }

    }
}


tmsLocToolLocation['$inject'] = ["locationService","carRecommendService"]
angular.module('tmsLocToolLocation', []).directive('tmsLocToolLocation', tmsLocToolLocation);